<?php
// +----------------------------------------------------------------------
// | ManniuRead
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2016 http://manniuyun.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: heitao <read@manniuyun.com>
// +----------------------------------------------------------------------

namespace app\member\controller\admin;

use manniu\controller\AdminBase;

use app\member\model\MemberGroup;
use app\member\model\Member as MemberModel;

class Group extends AdminBase{

	//当前控制器菜单
 	public $submenu = [];

 	/**
     * 构造函数
     */
    public function __construct() {
        parent::__construct();
        $this->submenu[] = ['title' => '会员组管理', 'url' => url('member/admin.group/index'), 'icon' => '&#xe60a;'];
        $this->submenu[] = ['title' => '会员组添加', 'url' => url('member/admin.group/add'), 'icon' => '&#xe654;'];
        $this->submenu[] = ['title' => '更新会员组缓存', 'url' => url('member/admin.group/cache'), 'icon' => 'ဂ'];
    }

	/**
	 * 后台会员组管理首页
	 */
    public function index(){
        $this->assign([
            'list' => MemberGroup::getGroupAll(),
            'submenu' => $this->submenu,
        ]);
        return $this->fetch();
    }

    /**
     * 会员组添加
     */
    public function add() {
        $member_group_model = new MemberGroup;
        if($this->request->isPost()){
            if($member_group_model->allowField(true)->validate(true)->save($this->request->param('data/a'))){
                $member_group_model->cache();
                $this->success('会员组添加成功','index');
            }else{
                $this->error($member_group_model->getError());
            }
        }
        $this->assign([
            'submenu' => $this->submenu,
        ]);
        return $this->fetch();
    }

    /**
     * 会员组修改
     */
    public function edit($id = 0) {
        $id = (int)$id;
        if(!$id){
            $this->error('缺少会员组ID');
        }
        $member_group = MemberGroup::get($id);
        if(!$member_group){
            $this->error('会员组不存在');
        }
        // 表单提交操作
        if($this->request->isPost()){
            if(false !== $member_group->allowField(true)->validate(true)->save($this->request->param('data/a'),['id'=>$id])){
                $member_group->cache();
                $this->success('会员组修改成功','index');
            }else{
                $this->error($member_group->getError());
            }
        }
        // 向模版赋值
        $this->assign([
            'data' => $member_group,
            'submenu' => $this->submenu,
        ]);
        return $this->fetch();  
    }

    /**
     * 会员组删除
     */
    public function del($id = 0) {
    	$id = (int)$id;
        if(!$id){
            $this->error('缺少会员组ID');
        }
        if($id == 1){
            $this->error('系统会员组不允许删除');
        }
        $member_group = MemberGroup::get($id);
        if(!$member_group){
            $this->error('会员组不存在');
        }

        //判断此会员组是否存在会员
        if(MemberModel::get(['group_id' => $member_group->id])){
            $this->error('此会员组里存在会员，不能删除');
        }

        $member_group->delete();
        $member_group->cache();
        $this->success('会员组删除成功','index');
    }

    /**
	 * 更新会员组缓存
	 */
    public function cache() {
        $member_group_model = new MemberGroup;
    	$member_group_model->cache();
    	$this->success('更新会员组缓存成功','index');
    }
}